<?php
 jimport('joomla.application.component.view');
 // Load the view framework
 if(!class_exists('VmView'))require(JPATH_COMPONENT_ADMINISTRATOR.DS.'helpers'.DS.'vmview.php');
 jimport('joomla.html.pane');
class svViewUseraccount extends JView
{
	function display()
	{
		$task = JRequest::getCMD('task');
		switch ($task)
		{
			case 'add':
				$this->_add();
				break;
			case 'edit':
				$this->_edit();
				break;
			default :
				$this->_default();
				break;
		}
		parent::display();
	}
	// hien thi chi tiet
	function _default()
	{
		// them toolbar
		$this->_gridToolbar();
		// ket noi voi model
		$model=$this->getModel('useraccount');
		$action=JRequest::getVar('action');
		if($action=='')
		{
			jimport('joomla.html.pagination');
			// phan trang
			$total=$model->getTotal();
			$view=JRequest::getVar('view');
			global $mainframe;
			$param['limitstart']=$mainframe->getUserStateFromRequest('com_svquanlycanbo.'.$view.'.limitstart', 'limitstart',0,'int');
			$param['limit']=$mainframe->getUserStateFromRequest('com_svquanlycanbo.'.$view.'.limit', 'limit',$mainframe->getCfg('list_limit'),'int');
			$pager=new JPagination($total, $param['limitstart'], $param['limit']);
			// get List
			$idcanbo=JRequest::getInt('sn_idcanbo');
			$donvi=JRequest::getInt('iddonvi');
			$phongban=JRequest::getInt('idphongban');
			$selected['canbo']=$idcanbo;
			$selected['donvi']=$donvi;
			$selected['phongban']=$phongban;
			$this->getList($selected);
			// cac bien gia tri
			$result=$model->getAllAccount($param);
			$title='Quản lý các tài khoản truy cập';
			// set layout
			$this->assignRef('dataObject', $result);
			$this->assignRef('title', $title);
			$this->assignRef('total', $total);
			$this->assignRef('pager', $pager);
			$this->assignRef('param', $param);
			$this->setLayout('default');
		}
		else
		{

			if($action=='add')
			{
				///--------------------------
				$user 		=& JUser::getInstance();
				$acl		=& JFactory::getACL();
				$gtree = $acl->get_group_children_tree( null, 'USERS', false );
				$lists['gid'] 	= JHTML::_('select.genericlist',   $gtree, 'gid', 'size="10"', 'value', 'text', $user->get('gid') );
				// build the html select list
				$lists['block'] 	= JHTML::_('select.booleanlist',  'block', 'class="inputbox" size="1"', $user->get('block') );
				// build the html select list
				$lists['sendEmail'] = JHTML::_('select.booleanlist',  'sendEmail', 'class="inputbox" size="1"', $user->get('sendEmail') );
				///--------------------------
				// get list
				$idcanbo=JRequest::getInt('sn_idcanbo');
				//if($idcanbo==0) $idcanbo=$object->sn_idcanbo;
				$donvi=JRequest::getInt('iddonvi');
				$phongban=JRequest::getInt('idphongban');
				$idloaisunghiep=$object->sn_idloaisunghiep;
				$selected['idloaisunghiep']=$idloaisunghiep;
				$selected['canbo']=$idcanbo;
				// select ten can bo
				$name=$model->selectName($idcanbo);
				//---------------------
				$selected['donvi']=$donvi;
				$selected['phongban']=$phongban;
				$this->getList($selected);
				///---------------------------
				$this->_formToolbar('Thêm mới một Account');
				// chon cac donviquanly
				$model=$this->getModel('useraccount');
				$iddonvi=JRequest::getInt('iddonvi');
				$idphongban=JRequest::getInt('idphongban');
				// set layout
				$this->assignRef('iddonvi', $iddonvi);
				$this->assignRef('namecb', $name);
				$this->assignRef('idphongban', $idphongban);
				$this->assignRef('lists', $lists);
				$this->setLayout('add');
			}
			else
			 {
			 	
			 	// toolbar
			 	$this->_formToolbar('Chỉnh sửa Account');
			 	// lay id tu form default
			 	$idaccount=JRequest::getInt('idaccount');
			 	// lay du lieu tu model
			 	$model=$this->getModel('useraccount');
			 	$account=$model->getAccountByID($idaccount);
			 	///--------------------------
			 	$user 		=& JUser::getInstance();
			 	$acl		=& JFactory::getACL();
			 	$gtree = $acl->get_group_children_tree( null, 'USERS', false );
			 	$lists['gid'] 	= JHTML::_('select.genericlist',   $gtree, 'gid', 'size="10"', 'value', 'text', $account->gid );
			 	// build the html select list
			 	$lists['block'] 	= JHTML::_('select.booleanlist',  'block', 'class="inputbox" size="1"', $account->block );
			 	// build the html select list
			 	$lists['sendEmail'] = JHTML::_('select.booleanlist',  'sendEmail', 'class="inputbox" size="1"', $account->sendEmail );
			 	// set layout
			 	$this->assignRef('account', $account);
			 	$this->assignRef('idaccount', $idaccount);
			 	$this->assignRef('lists', $lists);
			 	$this->setLayout('edit');
			}
		}

	}
	function getList($selected)
	{
		$model=$this->getModel('useraccount');
		// list can bo
		$option[]=array('key'=>0,'text'=>JText::_('Chọn cán bộ'));
		$array=$model->Listcanbo();
		$array=array_merge($option,$array);
		$this->array=$array;
		$attr='class="inputbox" onchange="submitform()" style="width:220px;"';
		$list['canbo']=JHTML::_('select.genericlist',$array,'sn_idcanbo',$attr,'key','text',$selected['canbo']);
		// list phong ban
		$option1[]=array('key'=>0,'text'=>JText::_('Chọn phòng ban'));
		$array1=$model->ListPhongban();
		$array1=array_merge($option1,$array1);
		$this->array=$array1;
		$list['phongban']=JHTML::_('select.genericlist',$array1,'idphongban',$attr,'key','text',$selected['phongban']);
		// list don vi
		$option2[]=array('key'=>0,'text'=>JText::_('Chọn đơn vị'));
		$array2=$model->ListDonvi();
		$array2=array_merge($option2,$array2);
		$this->array=$array2;
		$list['donvi']=JHTML::_('select.genericlist',$array2,'iddonvi',$attr,'key','text',$selected['donvi']);
		$this->list=$list;
	}
	function _add()
	{
		header('location:index.php?option=com_svquanlycanbo&view=useraccount&action=add');
	}
	function _edit()
	{
		$idlist    = JRequest::getVar('cid',array(0),'','array');
		JArrayHelper::toInteger($idlist, array(0));
		$idaccount     = $idlist[0];
		header('location:index.php?option=com_svquanlycanbo&view=useraccount&action=edit&idaccount='.$idaccount);
	}
	function _save()
	{
		// lay du lieu
		$idaccount=JRequest::getInt('idaccount');
		$idcanbo=JRequest::getInt('idcanbo');
		$username=JRequest::getVar('username');
		$password=JRequest::getVar('password');
		$email=JRequest::getVar('email');
		$typeaccount=JRequest::getVar('typeaccount');
		// xu ly yeu cau edit/insert
		$model=$this->getModel('useraccount');
		if($idaccount==0)// xac thuc viec them mot item
		{
			$exec=$model->addNewAccount($idcanbo,$username,$password,$email,$typeaccount);
		}
		else//xac thuc viec edit mot item
		{
			$exec=$model->editAccountt($idaccount,$idcanbo,$username,$password,$email,$typeaccount);
		}
		echo $exec;
		header('Location:index.php?option=com_svquanlycanbo&view=account');
	}
	function _cancel()
	{
		header('Location:index.php?option=com_svquanlycanbo&view=account');
	}
	function _remove()
	{
		// lay id tu form default
		$idlist    = JRequest::getVar('cid',array(0),'','array');
		JArrayHelper::toInteger($idlist, array(0));
		$idaccount    = $idlist[0];
		// thuc hien yeu cau remove
		$model=$this->getModel('useraccount');
		$exec=$model->delAccount($idaccount);
		echo $exec;
		$this->_default();
		
	}
	// them cac button tren thanh toolbar
	function _gridToolbar()
	{
		JToolBarHelper::title('Quản lý các phòng ban.!');
		JToolBarHelper::addNew();
		JToolBarHelper::editListX();
		JToolBarHelper::deleteList();
	}
	function _formToolbar($title)
	{
		JToolBarHelper::title($title);
		JToolBarHelper::save();
		JToolBarHelper::cancel();
	}
	
}
?>